﻿using System;
using System.Collections.Generic;
using System.Text;
using wojilu.ORM;

namespace wojilu.Data
{
    internal class SqliteTableBuilder : TableBuilderBase
    {
        protected override void addColumn_PrimaryKey(EntityInfo entity, StringBuilder sb, Dictionary<String, EntityInfo> clsList)
        {

            sb.Append(" [id] integer NOT NULL  PRIMARY KEY AUTOINCREMENT ,  ");

        }

        protected override void addColumn_Int(StringBuilder sb, EntityPropertyInfo ep, String columnName)
        {
            sb.Append(columnName);
            if (ep.Property.IsDefined(typeof(TinyIntAttribute), false))
            {
                sb.Append(" tinyint default 0, ");
            }
            else
            {
                sb.Append(" int default 0, ");
            }
        }

        protected override void addColumn_LongText(EntityInfo entity, StringBuilder sb, String columnName)
        {
            sb.Append(columnName);
            sb.Append(" text, ");
        }

        protected override void addColumn_ShortText(StringBuilder sb, String columnName, int length)
        {
            sb.Append(columnName);
            sb.Append(" varchar(");
            sb.Append(length);
            sb.Append("), ");
        }

        protected override void addColumn_Decimal(EntityInfo entity, StringBuilder sb, EntityPropertyInfo ep, String columnName)
        {

            if (ep.MoneyAttribute != null)
            {
                sb.Append(columnName);
                sb.Append(" decimal(19, 4) default 0, ");
            }
            else
            {

                DecimalAttribute da = ep.DecimalAttribute;
                if (da == null) throw new Exception("DecimalAttribute not found=" + entity.FullName + "_" + ep.Name);

                sb.Append(columnName);
                sb.Append(" decimal(" + da.Precision + "," + da.Scale + ") default 0, ");

            }

        }


        protected override void addColumn_Double(EntityInfo entity, StringBuilder sb, string columnName)
        {
            sb.Append(columnName);
            sb.Append(" double default 0, ");
        }


        protected override void addColumn_Single(EntityInfo entity, StringBuilder sb, string columnName)
        {
            sb.Append(columnName);
            sb.Append(" float default 0, ");
        }

    }
}
